package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.PermissionUtil;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        // 查询结果集
        List<Permission> list = permissionMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Autowired
    private ApplicationContext ctx;  // spring 上下文对象

    @Override
    public void reload() {
        // int i = 1/0;
        //获取所有的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        // 获取所有的 controller
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> values = map.values();
        for (Object controller : values) {
            // 获取字节码对象
            Class<?> clazz = controller.getClass();
            // 获取 每一个 controller里面的方法
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                // 判断该方法是否有贴自定义的权限注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                // 有，就保存权限数据到数据库
                // 没有，就直接跳过该方法，不进行处理
                // 如果该权限的表达式已经存在数据库，就不插入；不存在，插入
                if(annotation!=null) {
                    //获取注解中的权限名称name
                    String name = annotation.value()[1];
                    //通过反射获取权限表达式
                    /*String simpleName = clazz.getSimpleName().replace("Controller","");
                    String expression = StringUtils.uncapitalize(simpleName) + ":" + method.getName();
                    */
                    String expression = annotation.value()[0];
                    if(!permissions.contains(expression)) {
                        //封装成权限对象
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        //把权限对象保存到数据库
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmId(Long id) {
        return permissionMapper.selectByEmId(id);
    }
}
